<?php namespace App\Libraries;

class TxpagesLib
{
    /*   属性     */
    public $limit;                         //SQL语句使用limit从句,限制获取记录个数
    private $part = 4;                       //控制数字列表当前页前后链接数量
    private $totalpage = 0;                  //总页数
    private $url = '';                       //url地址,不含分页所在的段,形如：'control/function'
    private $total = 0;                      //总条数
    private $perpage = 10;                    //每页条数·
    private $nowindex = 1;                   //当前页
    //private $seg = 3;                        //页码参数取 index.php之后的段数，默认为3，即index.php/control/function/18 这种形式
    private $next_page = '下一页';          //下一页
    private $pre_page = '上一页';           //上一页
    private $first_page = '首页';           //首页
    private $last_page = '末页';            //尾页

    /* constructor构造函数 */
    public function __construct($params = array())
    {
        if (count($params) > 0) {
            $this->initialize($params);
        }
        log_message('debug', "MyPage Class Initialized");
    }

    /*初始化*/
    function initialize($params)
    {
        if (count($params) > 0) {
            $this->total = isset($params['total']) ? intval($params['total']) : 0;//总条数
            $this->perpage = isset($params['perpage']) ? intval($params['perpage']) : 5;//每页条数
            $this->nowindex = isset($params['nowindex']) ? intval($params['nowindex']) : 1;//当前页
            $this->url = (isset($params['url']) ? $params['url'] : '');//url地址,不含分页所在的段,形如：'control/function'
            $this->part = isset($params['part']) ? $params['part'] : 2;//控制数字列表当前页前后链接数量
            //$this->seg = isset($params['seg']) ? $params['seg'] : 3;//页码参数取 index.php之后的段数，默认为3，即index.php/control/function/18 这种形式
            $this->next_page = isset($params['next_page']) ? $params['next_page'] : '下一页';
            $this->pre_page = isset($params['pre_page']) ? $params['pre_page'] : '上一页';
            $this->first_page = isset($params['first_page']) ? $params['first_page'] : '首页';
            $this->last_page = isset($params['last_page']) ? $params['last_page'] : '末页';
        }
        $this->totalpage = ceil($this->total / $this->perpage);//总页数
        $this->_myset_url($this->url);//设置链接地址
        $this->limit = $this->setLimit();
    }

    /* 返回Limit数据库限制 */
    private function setLimit()
    {
        if ($this->nowindex > 0) {
            //用于返回Limit数据库限制，如： "10, 10" 或"10, 10" 或"20, 10"
            return 'LIMIT ' . ($this->nowindex - 1) * $this->perpage . ", {$this->perpage}";
        } else
            return 0;
    }

    /* 获取显示"下一页"的代码 */
    function next_page($style = 'pg_next')
    {
        if ($this->nowindex < $this->totalpage) {
            return $this->_get_link($this->_get_url($this->nowindex + 1), $this->next_page, $style);
        }
        return '<span class="' . $style . '">' . $this->next_page . '</span>';
    }

    /* 获取显示“上一页”的代码 */
    function pre_page($style = 'pg_pre')
    {
        if ($this->nowindex > 1) {
            return $this->_get_link($this->_get_url($this->nowindex - 1), $this->pre_page, $style);
        }
        return '<span class="' . $style . '">' . $this->pre_page . '</span>';
    }

    /* 获取显示“首页”的代码 */
    function first_page($style = 'pg_first')
    {
        if ($this->nowindex == 1) {
            return '<span class="' . $style . '">' . $this->first_page . '</span>';
        }
        return $this->_get_link($this->_get_url(1), $this->first_page, $style);
    }

    /* 获取显示“尾页”的代码 */
    function last_page($style = 'pg_last')
    {
        if ($this->nowindex == $this->totalpage) {
            return '<span class="' . $style . '">' . $this->last_page . '</span>';
        }
        return $this->_get_link($this->_get_url($this->totalpage), $this->last_page, $style);
    }

    /* 在对象内部使用的私有方法，用于获取页数列表信息 */
    private function pageList()
    {
        $linkPage = "&nbsp;<b>";

        $inum = floor($this->part / 2);

        //输出当前页面的前面几个页面
        for ($i = $inum; $i >= 1; $i--) {
            $page = $this->nowindex - $i;
            if ($page >= 1) {
                $linkPage .= "<a href='{$this->url}/{$page}'>{$page}</a>&nbsp;";
            }
        }
        //echo $linkPage;
        /*当前页的信息 */
        if ($this->totalpage > 1)
            $linkPage .= "<span style='padding:1px 2px;background: #546899;color:white;font-size:14px;'>{$this->nowindex}</span>&nbsp;";

        //输出当前页面的前面几个页面
        for ($i = 1; $i <= $inum; $i++) {
            $page = $this->nowindex + $i;
            if ($page <= $this->totalpage)
                $linkPage .= "<a href='{$this->url}/{$page}'>{$page}</a>&nbsp;";
            else
                break;
        }
        $linkPage .= '</b>';
        return $linkPage;
    }

    /*  获取显示跳转按钮的代码 */
    function select()
    {
        $out = '<select name="pagelect" class="pg_select">';
        for ($i = 1; $i <= $this->totalpage; $i++) {
            if ($i == $this->nowindex) {
                $out .= '<option value="' . $i . '" selected>' . $i . '</option>';
            } else {
                $out .= '<option value="' . $i . '">' . $i . '</option>';
            }
        }
        $out .= '</select>';
        return $out;
    }

    /* 在编辑框输入页面，点击回车跳转*/
    private function goPage()
    {
        if ($this->totalpage > 1) {
            $str = <<<EOT
           &nbsp;
           <input style="width:30px;height:17px !important;height:18px;border:1px solid #CCCCCC;" type="text" 
           onkeydown="javascript:if(event.keyCode==13)
           {var page=(this.value>{$this->totalpage})?{$this->totalpage}:this.value;location='{$this->url}/'+page+''}" value="{$this->nowindex}">
           &nbsp;
           <input style="cursor:pointer;width:45px;height:23px;border:1px solid #CCCCCC;" type="button" value="GO" 
           onclick="javascript:var page=(this.previousSibling.value>{$this->totalpage})?{$this->totalpage}:this.previousSibling.value;location='{$this->url}/'+page+''">
           &nbsp;
EOT;
            return $str;
        }
    }

    /* 控制分页显示风格 */
    function show($mode = 1)
    {
        switch ($mode) {
            case 1://上一页 1 2 3 4 5 下一页 第x页
                return '共 ' . $this->total . ' 条 ' . $this->pre_page() . '&nbsp' . $this->pageList() . '&nbsp' . $this->next_page();
                break;
            case 2://首页 上一页 1 2 3 4 5 下一页  末页 第x页
                return '共 ' . $this->total . ' 条 ' . $this->first_page() . '&nbsp' . $this->pre_page() . '&nbsp' . $this->pageList() . '&nbsp' . $this->next_page() . '&nbsp' . $this->last_page() . '&nbsp;第' . $this->select() . '页';
                break;
            case 3://上一页 1 2 3 4 5 下一页
                return '共 ' . $this->total . ' 条 ' . $this->pre_page() . '&nbsp' . $this->pageList() . '&nbsp' . $this->next_page();
                break;
            case 4://首页 上一页 1 2 3 4 5 下一页  末页 GO页
                return '共 ' . $this->total . ' 条 ' . $this->first_page() . '&nbsp' . $this->pre_page() . '&nbsp' . $this->pageList() . '&nbsp' . $this->next_page() . '&nbsp' . $this->last_page() . '&nbsp' . $this->goPage();
                break;
            default://上一页 1 2 3 4 5 下一页  第x页
                return '共 ' . $this->total . ' 条 ' . $this->pre_page() . '&nbsp' . $this->pageList() . '&nbsp' . $this->next_page() . '&nbsp;第' . $this->select() . '页';
                break;
        }
    }

    /* 设置url头地址 */
    public function _myset_url($url)
    {
        //$this->url = base_url('public/index.php/') . $url;
        $this->url = base_url($url);
    }

    /* 为指定的页面返回地址值 */
    function _get_url($pagenum = 1)
    {
        return $this->url . '/' . $pagenum;
    }

    /* 获取链接地址  */
    function _get_link($url, $text, $style = '')
    {
        $style = $style ? 'class="' . $style . '"' : '';
        return '<a ' . $style . ' href="' . $url . '">' . $text . '</a>';
    }
}

?>